package com.freesky.biz.abchina.service.impl;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.freesky.biz.abchina.domain.TMonthGoldRecord;
import com.freesky.biz.abchina.domain.TXianglanProduct;
import com.freesky.biz.abchina.domain.TXianglanRecord;
import com.freesky.biz.abchina.mapper.MonthGoldRecordMapper;
import com.freesky.biz.abchina.mapper.XianglanRecordMapper;
import com.freesky.biz.abchina.service.IMonthGoldRecordService;
import com.freesky.biz.abchina.service.IXianglanRecordService;
import com.freesky.biz.utils.SeqUtil;
import com.freesky.common.core.domain.AjaxResult;
import com.freesky.common.core.domain.entity.SysUser;
import com.freesky.common.core.redis.RedisCache;
import com.freesky.common.enums.ExportStatusEnum;
import com.freesky.common.utils.SecurityUtils;
import com.freesky.system.domain.TExportRecord;
import com.freesky.system.service.ITExportRecordService;
import com.freesky.system.task.SyncReport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class XianglanRecordServiceImpl implements IXianglanRecordService {

    @Autowired
    private XianglanRecordMapper xianglanRecordMapper;

    @Autowired
    private ITExportRecordService exportRecordService;

    @Autowired
    private SyncReport syncReport;

    @Autowired
    private RedisCache redisCache;


    @Override
    public List<TXianglanRecord> findList(TXianglanRecord entity) {
        return xianglanRecordMapper.findList(entity);
    }

    @Override
    public AjaxResult exportFile(TXianglanRecord entity) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        TExportRecord exportRecord = new TExportRecord();
        exportRecord.setId(SeqUtil.getSeqId());
        exportRecord.setBizType("XIANG_LAN");
        exportRecord.setCreateBy(String.valueOf(sysUser.getUserId()));

        TExportRecord exportRecordSelect = exportRecordService.selectRecordExportByStatus(exportRecord);
        if (exportRecordSelect != null) {
            return AjaxResult.error("已经有下载任务");
        }

        exportRecord.setStatus(ExportStatusEnum.SUBMIT_WAIT.getValue());
        exportRecordService.insert(exportRecord);

        syncReport.exportFileMutilHeader(entity, new TXianglanRecord(), IXianglanRecordService.class,
                "findList", "向岚",Long.parseLong(exportRecord.getId()),sysUser.getUserName());
        return AjaxResult.success();
    }

    @Override
    public List<TXianglanProduct> selectXianglanProductList(String startDt) {
        return xianglanRecordMapper.selectXianglanProductList(startDt);
    }

    @Override
    public List<TXianglanRecord> selectUnPayList() {
        return xianglanRecordMapper.selectUnPayList();
    }

    @Override
    public int updatePayStatus(TXianglanRecord entity) {
        return xianglanRecordMapper.updatePayStatus(entity);
    }

    @Override
    public void xianglanRecordInit() {
        String startDt = DateUtil.format(DateUtil.beginOfWeek(new Date()),"yyyy-MM-dd");
        List<TXianglanProduct> xianglanProductList = xianglanRecordMapper.selectXianglanProductList(startDt);
        for(int i=0;i<xianglanProductList.size();i++){
            TXianglanProduct xianglanProduct = xianglanProductList.get(i);
            Integer leftCount = xianglanProduct.getTotalCount() - xianglanProduct.getUseCount();
            xianglanProduct.setLeftCount(leftCount);
            xianglanProductList.set(i,xianglanProduct);
            setXianglanProductToRedis("ABCHINA:PD:"+xianglanProduct.getType(),xianglanProduct);
        }
        //总产品列表
        redisCache.setCacheObject("ABCHINA:PDLIST",JSON.toJSONString(xianglanProductList));
    }

    private void setXianglanProductToRedis(String key, TXianglanProduct xianglanProduct) {
        //System.out.println("setXianglanProductToRedis:"+key+","+ JSON.toJSONString(xianglanProduct));
        redisCache.hset(key, "id", xianglanProduct.getId());
        redisCache.hset(key, "name", xianglanProduct.getName());
        redisCache.hset(key, "externalFlag", xianglanProduct.getExternalFlag());
        redisCache.hset(key, "userLevel", xianglanProduct.getUserLevel());
        redisCache.hset(key, "type", xianglanProduct.getType());
        redisCache.hset(key, "amt", xianglanProduct.getAmt());
        redisCache.hset(key, "stockId", xianglanProduct.getStockId());
        redisCache.hset(key, "totalCount", xianglanProduct.getTotalCount());
        redisCache.hset(key, "useCount", xianglanProduct.getUseCount());
        redisCache.hset(key, "leftCount", xianglanProduct.getLeftCount());
    }

}
